﻿using Calligraphy.CalligraphyData;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;

namespace Calligraphy.DataModel
{
    class SearchResultSource
    {
        ObservableCollection<object> photoes;

        public ObservableCollection<object> Photoes
        {
            get { return photoes; }
            set { photoes = value; }
        }
        public SearchResultSource()
        { }



        public async Task<ObservableCollection<Object>> SearchPhoto(string queryText)
        {
            photoes = new ObservableCollection<object>();
            var searchInPaper =
                //    await
                //    ((DataServiceQuery<T_PaperPhoto>)DataServiceContextHelper.DataContext.CreateQuery<T_PaperPhoto>("SearchPaperPhoto").AddQueryOption("queryText", string.Format("'{0}'", queryText))).ExecuteAsync<T_PaperPhoto>();
                //var searchInRubbing =
                //    await
                //    ((DataServiceQuery<T_RubbingPhoto>)DataServiceContextHelper.DataContext.CreateQuery<T_RubbingPhoto>("SearchRubbingPhoto").AddQueryOption("queryText", string.Format("'{0}'", queryText))).ExecuteAsync<T_RubbingPhoto>();

            await
            ((DataServiceQuery<T_PaperPhoto>)DataServiceContextHelper.DataContext.T_PaperPhoto.Expand("T_Photo").Expand("T_StelePaper").Expand("T_StelePaper/T_Author").Where(p => p.Text.Contains(queryText) && (p.IsDeleted == null || p.IsDeleted == false))).ExecuteAsync<T_PaperPhoto>();
            var searchInRubbing =
                await
                ((DataServiceQuery<T_RubbingPhoto>)DataServiceContextHelper.DataContext.T_RubbingPhoto.Expand("T_Photo").Expand("T_Rubbing").Expand("T_Rubbing/T_StelePaper").Expand("T_Rubbing/T_StelePaper/T_Author").Where(p => p.Text.Contains(queryText) && (p.IsDeleted == null || p.IsDeleted == false))).ExecuteAsync<T_RubbingPhoto>();

            photoes = OrderByDynast(searchInRubbing, searchInPaper);
            return photoes;
        }

        public ObservableCollection<object> FilterSearchResult(string selectedAuthor, string selectedStyle, CollectionViewSource searchResult)
        {

            ObservableCollection<T_RubbingPhoto> rubbingPhotoes = new ObservableCollection<T_RubbingPhoto>();
            ObservableCollection<T_PaperPhoto> paperPhotoes = new ObservableCollection<T_PaperPhoto>();
            ObservableCollection<object> filterResult = new ObservableCollection<object>();
            ObservableCollection<object> withoutDynast = new ObservableCollection<object>();
            if (selectedAuthor == "全部书法家")
            {
                filterResult = FiterWithAllAuthor(selectedStyle, searchResult);
            }
            else if (selectedStyle == "全部字体")
            {
                filterResult = FilterWithAllStyle(selectedAuthor, searchResult);
            }
            else
                filterResult = FilterWithExactlyAuthorAndStylt(selectedAuthor.Trim(), selectedStyle.Trim(), searchResult);
            return filterResult;
        }

        private ObservableCollection<object> FilterWithExactlyAuthorAndStylt(string selectedAuthor, string selectedStyle, CollectionViewSource searchResult)
        {
            ObservableCollection<T_RubbingPhoto> rubbingPhotoes = new ObservableCollection<T_RubbingPhoto>();
            ObservableCollection<T_PaperPhoto> paperPhotoes = new ObservableCollection<T_PaperPhoto>();
            ObservableCollection<object> filterResult = new ObservableCollection<object>();
            ObservableCollection<object> withoutDynast = new ObservableCollection<object>();
            IEnumerable<object> filtInRubbingPhotoes;
            IEnumerable<object> filtInPaperPhotoes;
            foreach (var photo in searchResult.View.ToArray())
            {
                if (photo is T_RubbingPhoto && (photo as T_RubbingPhoto).T_Rubbing.T_StelePaper != null && (photo as T_RubbingPhoto).T_Rubbing.T_StelePaper.T_Author != null
                    && (photo as T_RubbingPhoto).T_Rubbing.T_StelePaper.T_Style != null)
                    rubbingPhotoes.Add(photo as T_RubbingPhoto);
                else if (photo is T_PaperPhoto && (photo as T_PaperPhoto).T_StelePaper != null && (photo as T_PaperPhoto).T_StelePaper.T_Author != null &&
                    (photo as T_PaperPhoto).T_StelePaper.T_Style != null)
                    paperPhotoes.Add(photo as T_PaperPhoto);
            }

            filtInRubbingPhotoes =
                 from rp in rubbingPhotoes
                 where rp.T_Rubbing.T_StelePaper.T_Author.Name.Trim() == selectedAuthor && rp.T_Rubbing.T_StelePaper.T_Style.Name.Trim() == selectedStyle
                 select rp;
            filtInPaperPhotoes =
                 from pp in paperPhotoes
                 where pp.T_StelePaper.T_Author.Name.Trim() == selectedAuthor && pp.T_StelePaper.T_Style.Name.Trim() == selectedStyle
                 select pp;
            //foreach (var rp in filtInRubbingPhotoes)
            //{
            //    filterResult.Add(rp);
            //}
            //foreach (var pp in filtInPaperPhotoes)
            //{
            //    filterResult.Add(pp);
            //}
            filterResult = OrderByDynast(filtInRubbingPhotoes, filtInPaperPhotoes);
            return filterResult;
        }

        private ObservableCollection<object> FilterWithAllStyle(string selectedAuthor, CollectionViewSource searchResult)
        {
            ObservableCollection<T_RubbingPhoto> rubbingPhotoes = new ObservableCollection<T_RubbingPhoto>();
            ObservableCollection<T_PaperPhoto> paperPhotoes = new ObservableCollection<T_PaperPhoto>();
            ObservableCollection<object> filterResult = new ObservableCollection<object>();
            ObservableCollection<object> withoutDynast = new ObservableCollection<object>();
            IEnumerable<object> filtInRubbingPhotoes;
            IEnumerable<object> filtInPaperPhotoes;
            foreach (var photo in searchResult.View.ToArray())
            {
                if (photo is T_RubbingPhoto && (photo as T_RubbingPhoto).T_Rubbing.T_StelePaper != null && (photo as T_RubbingPhoto).T_Rubbing.T_StelePaper.T_Author != null
                    && (photo as T_RubbingPhoto).T_Rubbing.T_StelePaper.T_Style != null)
                    rubbingPhotoes.Add(photo as T_RubbingPhoto);
                else if (photo is T_PaperPhoto && (photo as T_PaperPhoto).T_StelePaper != null && (photo as T_PaperPhoto).T_StelePaper.T_Author != null &&
                    (photo as T_PaperPhoto).T_StelePaper.T_Style != null)
                    paperPhotoes.Add(photo as T_PaperPhoto);
            }
            filtInRubbingPhotoes =
                 from rp in rubbingPhotoes
                 where rp.T_Rubbing.T_StelePaper.T_Author.Name.Trim() == selectedAuthor
                 //orderby rp.T_Rubbing.T_StelePaper.T_Dynast.ID
                 select rp;
            filtInPaperPhotoes =
                 from pp in paperPhotoes
                 where pp.T_StelePaper.T_Author.Name.Trim() == selectedAuthor
                 orderby pp.T_StelePaper.T_Dynast.ID
                 select pp;

            filterResult = OrderByDynast(filtInRubbingPhotoes, filtInPaperPhotoes);

            return filterResult;
        }

        private ObservableCollection<object> FiterWithAllAuthor(string selectedStyle, CollectionViewSource searchResult)
        {
            ObservableCollection<T_RubbingPhoto> rubbingPhotoes = new ObservableCollection<T_RubbingPhoto>();
            ObservableCollection<T_PaperPhoto> paperPhotoes = new ObservableCollection<T_PaperPhoto>();
            ObservableCollection<object> filterResult = new ObservableCollection<object>();
            ObservableCollection<object> withoutDynast = new ObservableCollection<object>();
            IEnumerable<object> filtInRubbingPhotoes;
            IEnumerable<object> filtInPaperPhotoes;
            foreach (var photo in searchResult.View.ToArray())
            {
                if (photo is T_RubbingPhoto && (photo as T_RubbingPhoto).T_Rubbing.T_StelePaper != null && (photo as T_RubbingPhoto).T_Rubbing.T_StelePaper.T_Author != null
                    && (photo as T_RubbingPhoto).T_Rubbing.T_StelePaper.T_Style != null)
                    rubbingPhotoes.Add(photo as T_RubbingPhoto);
                else if (photo is T_PaperPhoto && (photo as T_PaperPhoto).T_StelePaper != null && (photo as T_PaperPhoto).T_StelePaper.T_Author != null &&
                    (photo as T_PaperPhoto).T_StelePaper.T_Style != null)
                    paperPhotoes.Add(photo as T_PaperPhoto);
            }
            filtInRubbingPhotoes =
                     from rp in rubbingPhotoes
                     where rp.T_Rubbing.T_StelePaper.T_Style.Name.Trim() == selectedStyle
                     select rp;
            filtInPaperPhotoes =
                 from pp in paperPhotoes
                 where pp.T_StelePaper.T_Style.Name.Trim() == selectedStyle
                 select pp;
            filterResult = OrderByDynast(filtInRubbingPhotoes, filtInPaperPhotoes);
            return filterResult;
        }
        public ObservableCollection<object> OrderByDynast(IEnumerable<object> rubbingPhotoes, IEnumerable<object> paperPhotoes)
        {

            Array rubbingPhotoess;
            Array paperPhotoess;
            rubbingPhotoess = rubbingPhotoes.ToArray();
            paperPhotoess = paperPhotoes.ToArray();
            ObservableCollection<object> results = new ObservableCollection<object>();
            ObservableCollection<object> withoutDynast = new ObservableCollection<object>();
            int j = 0;
            if (rubbingPhotoess.Length == 0)
            {
                foreach (var p in paperPhotoess)
                {
                    results.Add(p);
                }
            }
            else if (paperPhotoess.Length == 0)
            {
                foreach (var p in rubbingPhotoess)
                {
                    results.Add(p);
                }
            }
            else
            {
                //for (int i = 0; i < rubbingPhotoess.Length; i++)
                //{
                //    if ((rubbingPhotoess.GetValue(i) as T_RubbingPhoto).T_Rubbing.T_StelePaper.T_Dynast == null)
                //        withoutDynast.Add(rubbingPhotoess.GetValue(i));
                //    else
                //    {
                //        for (; j < paperPhotoess.Length; j++)
                //        {
                //            if ((paperPhotoess.GetValue(j) as T_PaperPhoto).T_StelePaper.T_Dynast == null)
                //                withoutDynast.Add(paperPhotoess.GetValue(j));
                //            else if ((rubbingPhotoess.GetValue(i) as T_RubbingPhoto).T_Rubbing.T_StelePaper.T_Dynast.ID > (paperPhotoess.GetValue(j) as T_PaperPhoto).T_StelePaper.T_Dynast.ID)
                //            {
                //                results.Add(paperPhotoess.GetValue(j));
                //            }
                //            else
                //            {
                //                results.Add(rubbingPhotoess.GetValue(i));
                //                break;
                //            }
                //        }
                //    }
                //}
                int i = 0;
                for (; i < rubbingPhotoess.Length && j < paperPhotoess.Length; )
                {
                    if ((rubbingPhotoess.GetValue(i) as T_RubbingPhoto).T_Rubbing.T_StelePaper.T_Dynast == null)
                    {
                        withoutDynast.Add(rubbingPhotoess.GetValue(i));
                        i++;
                    }
                    else if ((paperPhotoess.GetValue(j) as T_PaperPhoto).T_StelePaper.T_Dynast == null)
                    {
                        withoutDynast.Add(paperPhotoess.GetValue(j));
                        j++;
                    }
                    else if ((rubbingPhotoess.GetValue(i) as T_RubbingPhoto).T_Rubbing.T_StelePaper.T_Dynast.ID > (paperPhotoess.GetValue(j) as T_PaperPhoto).T_StelePaper.T_Dynast.ID)
                    {
                        results.Add(paperPhotoess.GetValue(j));
                        j++;
                    }
                    else
                    {
                        results.Add(rubbingPhotoess.GetValue(i));
                        i++;
                    }
                }
                //int rubbingPhotoCount = 0;
                //int paperPhotoCount = 0;
                //foreach (var p in results)
                //{
                //    if (p is T_PaperPhoto)
                //        paperPhotoCount++;
                //    else
                //        rubbingPhotoCount++;

                //} 
                //for (; paperPhotoCount < paperPhotoess.Length; paperPhotoCount++)
                //{
                //    results.Add(paperPhotoess.GetValue(paperPhotoCount - 1));
                //}

                for (; i < rubbingPhotoess.Length; i++)
                {
                    results.Add(paperPhotoess.GetValue(i - 1));
                }
                for (; j < paperPhotoess.Length; j++)
                {
                    results.Add(paperPhotoess.GetValue(j));
                }
                foreach (var p in withoutDynast)
                {
                    results.Add(p);
                }
                //if (results.Count() < (rubbingPhotoess.Length + paperPhotoess.Length))
                //{
                //    int i = results.Count() - paperPhotoess.Length ;
                //    for (; i < rubbingPhotoess.Length; i++)
                //    {
                //        results.Add(rubbingPhotoess.GetValue(i));
                //    }
                //}
            }


            return results;
        }
    }

       

    }

